package client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import common.Buffer;
import common.Sender;

public class Shell extends Thread{

	private Sender sender;
	private Buffer input;
	private String printstring;
	
	public Shell(Sender sender, Buffer input, String printstring){
		this.sender = sender;
		this.input = input;
		this.printstring = printstring;
	}
	
	public void run(){
		while(true){
			String cmd = read();
			sender.send(cmd);
			String info = input.poll();
			while(info != null){
				print(info + "\n");
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				info = input.tryPoll();
			}
		}
	}
	
	private String read(){
		print(printstring + " ");
		InputStreamReader converter = new InputStreamReader(System.in);
		BufferedReader in = new BufferedReader(converter);
		try {
			return in.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private void print(String text){
		System.out.print(text);
	}
}
